//
//  CVTextView.m
//  eConsume
//
//  Created by haoyi on 14-3-7.
//  Copyright (c) 2014年 Hooy1. All rights reserved.
//

#import "CVTextView.h"

@interface CVTextView ()

- (void)_initialize;
- (void)_updateShouldDrawPlaceholder;
- (void)_textChanged:(NSNotification *)notification;

@end


@implementation CVTextView {
    
    BOOL _shouldDrawPlaceholder;
}

#pragma mark - Accessors

@synthesize placeholder = _placeholder;
@synthesize placeholderTextColor = _placeholderTextColor;

- (void)setText:(NSString *)string {
    
    [super setText:string];
    [self _updateShouldDrawPlaceholder];
}


- (void)setPlaceholder:(NSString *)string {
    
    if ([string isEqual:_placeholder]) return;
    
    _placeholder = string;
    [self _updateShouldDrawPlaceholder];
}

#pragma mark - NSObject
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}


#pragma mark - UIView

- (id)initWithCoder:(NSCoder *)aDecoder {
    
    if ((self = [super initWithCoder:aDecoder])) {
        
        [self _initialize];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    
    if ((self = [super initWithFrame:frame])) {
        
        [self _initialize];
    }
    
    return self;
}


- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    if (_shouldDrawPlaceholder) {
        
        [_placeholderTextColor set];
        [_placeholder drawInRect:CGRectMake(8.0f, 8.0f,
                                            self.frame.size.width - 16.0f,
                                            self.frame.size.height - 16.0f)
                        withFont:self.font];
//        [_placeholder drawInRect:CGRectMake(0.0f, 8.0f,
//                                            self.frame.size.width,
//                                            self.frame.size.height - 16.0f)
//                        withFont:self.font];
    }
}


#pragma mark - Private
- (void)_initialize {
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_textChanged:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:self];
    
    self.placeholderTextColor = [UIColor colorWithWhite:0.702f alpha:1.0f];
    _shouldDrawPlaceholder = NO;
}


- (void)_updateShouldDrawPlaceholder {
    
    BOOL prev = _shouldDrawPlaceholder;
    _shouldDrawPlaceholder = self.placeholder && self.placeholderTextColor && self.text.length == 0;
    
    if (prev != _shouldDrawPlaceholder) {
        
        [self setNeedsDisplay];
    }
}


- (void)_textChanged:(NSNotification *)notificaiton {
    
    [self _updateShouldDrawPlaceholder];
}

@end
